 package com.example.jqying.androiddemo.fragment;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.jqying.androiddemo.R;

public class ContainerActivity extends AppCompatActivity implements AFragment.IOMessageClick {

    private AFragment mAFragment;
    private BFragment bFragment;
    private Button mBtnChange;
    private Button mBtnResetText;
    private TextView mTvTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);






        mBtnChange = (Button) findViewById(R.id.btn_change) ;
        mBtnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(bFragment == null) {
                    bFragment = new BFragment();
                }
               getFragmentManager().beginTransaction().replace(R.id.fg_container, bFragment).commitAllowingStateLoss();
            }
        });

        //实例化Fragment
        mAFragment = AFragment.newInstance("Hello AFragment");
        //把Fragment放进容器中，调用commit
        getFragmentManager().beginTransaction().add(R.id.fg_container,mAFragment).commitAllowingStateLoss();

        mTvTitle = (TextView) findViewById(R.id.tv_title);

    }
    public void setData(String data)
    {
        mTvTitle.setText(data);
    }


    @Override
    public void onClick(String text) {
        mTvTitle.setText(text);
    }
}
